﻿using Infinity.BusinessLayer;
using Infinity.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Infinity.Areas.Managerial.Controllers
{
    public class GroupTypeController : Controller
    {

        InfinityEntities context = new InfinityEntities();
        //
        // GET: /Managerial/GroupType/
        public ActionResult Index()
        {
            List<grouptypeViewModel> list = context.Group_type.Select(c => new grouptypeViewModel
            {
                type = c.type,
                GroupTypeID = c.GroupTypeID
            }).ToList();
            return View(list);
        }
        public ActionResult editegrouptype(int ID)
        {

            Group_type group = context.Group_type.Where(c => c.GroupTypeID == ID).FirstOrDefault();
            grouptypeViewModel grouptype = new grouptypeViewModel
            {
                GroupTypeID = group.GroupTypeID,
                type = group.type
            };
            return View(grouptype);
        }

        public ActionResult Updategrouptype(grouptypeViewModel group)
        {
            context.SP_UpdateGroup_type(group.GroupTypeID, group.type);
            return View("editegrouptype", group); 
        }
        public ActionResult Deletegrouptype(int ID)
        {
            context.Group_type.Remove(context.Group_type.Where(c => c.GroupTypeID == ID).FirstOrDefault());
            context.SaveChanges();
            List<grouptypeViewModel> list = context.Group_type.Select(c => new grouptypeViewModel
            {
                type = c.type,
                GroupTypeID = c.GroupTypeID
            }).ToList();
            return View("_grouptypeList", list);
        }

        public ActionResult Newgrouptype()
        {
            grouptypeViewModel cat = new grouptypeViewModel();
            Random rnd = new Random();
            cat.GroupTypeID = rnd.Next(1, 13000);
            return View(cat);
        }

        [HttpPost]
        public ActionResult Newgrouptype(grouptypeViewModel group)
        {
            if (ModelState.IsValid)
            {
                Group_type c = new Group_type();
                c.type = group.type;
                c.GroupTypeID = group.GroupTypeID;
                context.Group_type.Add(c);
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            else return View(group);
        }



    }
}
